﻿using System;
using System.Collections.Generic;
using System.Text;
using VSAddinLibrary.Controllers;
using System.Reflection;
using VSAddinLibrary.Attributes;

namespace VSAddinLibrary.Utils
{
  /// <summary>
  /// 
  /// </summary>
  public class AddinReflector
  {
    public static List<Type> GetVSCommandTypes(Assembly assembly)
    {
      List<Type> result = new List<Type>();
      foreach (Type type in assembly.GetTypes())
      {
        if (!type.IsAbstract && typeof(IVSCommand).IsAssignableFrom(type))
        {
          if (FlagAttribute.GetFlagValue<VSCommandDeclarationAttribute>(type))
          {
            result.Add(type);
          }
        }
      }

      return result;
    }

    public static T GetAttrib<T>(Type type) where T : class
    {
      object[] attributes = type.GetCustomAttributes(typeof(T), true);
      if (attributes.Length == 0)
      {
        return default(T);
      }
      return attributes[0] as T;
    }
  }
}
